from django.shortcuts import render
from rest_framework import viewsets, status
from rest_framework.response import Response

from .models import Student,Classes
from .serializers import StudentSerializer


# Create your views here.

class StudentViewSet(viewsets.ViewSet):
    def list(self, request):
        #从模型得到学生数据查询集[(模型对象)，()..]所有学生
        students=Student.objects.all()
        # 通过序列器进行序列化 把查询集转json格式
        # many=True 代表多条数据转json格式数据 serializer 序列化器
        serializer = StudentSerializer(students, many=True)
        # data 属性保存了系列化后的数据，serializer.data 存储的是json 格式数据
        return Response(serializer.data)

    def create(self, request):
        serializer = StudentSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)

